layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
	
    /**
     * 空闲车
     */
    var BasicPrivatecar = {
        tableId: "basicPrivatecarTable"
    };

    /**
     * 初始化表格的列
     */
    BasicPrivatecar.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: 'Primary Key'},
            {align: 'center', toolbar: '#tableBar', title: 'Operations'},
            {field: 'privatecarState', title: 'Status',templet: '#billstateTpl'},
            {field: 'privatecarFleetName', title: 'Carrier', style:'font-weight: 600;'},
            {field: 'privatecarCarnumber', title: 'Vehicle Number', style:'font-weight: 600;'},
            {field: 'driver1', title: 'Driver', style:'font-weight: 600;'},
            {field: 'tel1', title: 'Phone', style:'font-weight: 600;'},
            {field: 'privatecarType', title: 'Vehicle Type', style:'font-weight: 600;'},
            {field: 'privatecarSurevolume', title: 'Carrying Capacity (m³)', style:'font-weight: 600;', sort: true},
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicPrivatecar.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['privatecarFleetId'] = $("#privatecarFleetId").val();
        table.reload(BasicPrivatecar.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicPrivatecar.tableId,
        url: Feng.ctxPath + '/orderOutcountXlb/privatecarlist',
        page: true,
        limit: 40,
        height: "full-60",
        cellMinWidth: 100,
        cols: BasicPrivatecar.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicPrivatecar.search();
    });

    // 工具条点击事件
    table.on('tool(' + BasicPrivatecar.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'xianze') {
			let cids = []
            for (var i = 0; i < parent.list2.length; i++) {
				cids.push(parent.list2[i].idi)
			}
            layer.confirm('Are you sure to select this vehicle?', function(index){
            	layer.close(index);
            	var msg;
                $.ajax({
                	url:Feng.ctxPath + "/orderOutcountXlb/batchassign?cids="+cids,
					data:{carId:data.id},
                	cache: false,
                	dataType:"json",
                	beforeSend:function(){
                		msg = layer.msg('Submitting... Please wait patiently', {
                			  icon: 16,
                			  shade: [0.2,'#000'], //0.1透明度的白色背景
                			  time: false
                		});
                	},
                	success:function(data){
                    	layer.close(msg); 
                		if (data.success) {
							parent.submit = 1
			            	Feng.success("Submission successful!");
					    	var Index = parent.layer.getFrameIndex(window.name);
					        parent.layer.close(Index);
						} else {
  		                    Feng.error("Submission failed! "+data.message);
						}
                	},
                	error:function(data){
                    	layer.close(msg); 
                        Feng.error("Submission failed! " + data.responseJSON.message)
                	}
                })
            }); 
        } 
    });

});
